<!DOCTYPE html>
<html lang="cn" class="js csstransforms3d">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta http-equiv="Cache-Control" content="max-age=7200" />
    <meta name="generator" content="Hugo 0.70.0" />
    <meta name="description" content="">
    <meta name="buildDate" content="2020-07-30 15:35:13">


    <link rel="shortcut icon" href="https://shardingsphere.apache.org/document/current/img/favicon.png" type="image/x-icon" />

    <title>Apache ShardingSphere整合Seata AT分布式事务 :: ShardingSphere</title>
    
    
    <link href="https://shardingsphere.apache.org/blog/css/nucleus.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/font-awesome.min.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/hybrid.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/featherlight.min.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/perfect-scrollbar.min.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/auto-complete.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/theme.css?1596123313" rel="stylesheet">
    <link href="https://shardingsphere.apache.org/blog/css/hugo-theme.css?1596123313" rel="stylesheet">
    
      <link href="https://shardingsphere.apache.org/blog/css/theme-black.css?1596123313" rel="stylesheet">
    

    <script src="https://shardingsphere.apache.org/blog/js/jquery-2.x.min.js?1596123313"></script>
    
    <style type="text/css">
      :root #header + #content > #left > #rlblock_left{ 
          display:none !important;
      }
      
    </style>
    

  </head>
  <body class="" data-url="/cn/material/seata/">
    <nav id="sidebar" class="">



  <div id="header-wrapper">
    <div id="header">
      <a href="https://shardingsphere.apache.org/"><img src="https://shardingsphere.apache.org/document/current/img/logo_v2.png" /></a>

    </div>
    
  </div>

    <div class="highlightable">
    <ul class="topics">

        
          
          


 
  
    
    <li data-nav-id="/cn/material/" title="博文选集" class="dd-item 
        parent
        
        
        ">
      <a href="https://shardingsphere.apache.org/blog/cn/material/">
          博文选集
          
      </a>
      
      
        <ul>
          
          
          
          
        
          
            
            


 
  
    
      <li data-nav-id="/cn/material/elasticjob/" title="快讯！分布式调度项目ElasticJob即将重新起航" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/elasticjob/">
        快讯！分布式调度项目ElasticJob即将重新起航
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/graduate/" title="分布式数据库解决方案Apache ShardingSphere毕业成为顶级项目" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/graduate/">
        分布式数据库解决方案Apache ShardingSphere毕业成为顶级项目
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/database/" title="我们是怎样打造一款分布式数据库的" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/database/">
        我们是怎样打造一款分布式数据库的
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/community/" title="Apache ShardingSphere社区的探索与拓展" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/community/">
        Apache ShardingSphere社区的探索与拓展
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/cncf/" title="快讯！Apache ShardingSphere进入CNCF全景图" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/cncf/">
        快讯！Apache ShardingSphere进入CNCF全景图
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/seata/" title="Apache ShardingSphere整合Seata AT分布式事务" class="dd-item active">
        <a href="https://shardingsphere.apache.org/blog/cn/material/seata/">
        Apache ShardingSphere整合Seata AT分布式事务
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/solution/" title="刚柔并济的开源分布式事务解决方案" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/solution/">
        刚柔并济的开源分布式事务解决方案
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/committer/" title="成为Apache官方认可的Committer有什么优势" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/committer/">
        成为Apache官方认可的Committer有什么优势
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/engine/" title="自动化执行引擎" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/engine/">
        自动化执行引擎
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/result/" title="剖析Sharding-Sphere系列——结果归并 " class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/result/">
        剖析Sharding-Sphere系列——结果归并 
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/realization/" title="分布式事务在Sharding-Sphere中的实现" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/realization/">
        分布式事务在Sharding-Sphere中的实现
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/material/proxy/" title="揭秘Sharding-Proxy——面向DBA的数据库中间层" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/material/proxy/">
        揭秘Sharding-Proxy——面向DBA的数据库中间层
        
        </a>
    </li>
     
  
 

            
          
        
        </ul>
              
    </li>
  
 

          
          


 
  
    
    <li data-nav-id="/cn/videos/" title="视频选集" class="dd-item 
        
        
        
        ">
      <a href="https://shardingsphere.apache.org/blog/cn/videos/">
          视频选集
          
      </a>
      
      
        <ul>
          
          
          
          
        
          
            
            


 
  
    
      <li data-nav-id="/cn/videos/opensource/" title="【开源总动员】零基础入门Apache ShardingSphere开源之道" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/videos/opensource/">
        【开源总动员】零基础入门Apache ShardingSphere开源之道
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/videos/build/" title="ShardingSphere的Apache共建之道" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/videos/build/">
        ShardingSphere的Apache共建之道
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/videos/new_sql/" title="从New SQL角度看Apache ShardingSphere" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/videos/new_sql/">
        从New SQL角度看Apache ShardingSphere
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/videos/evolution/" title="开源推动下的Apache ShardingSphere架构演进" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/videos/evolution/">
        开源推动下的Apache ShardingSphere架构演进
        
        </a>
    </li>
     
  
 

            
          
            
            


 
  
    
      <li data-nav-id="/cn/videos/ecosystem/" title="海量数据下的NewSQL数据库生态构建" class="dd-item ">
        <a href="https://shardingsphere.apache.org/blog/cn/videos/ecosystem/">
        海量数据下的NewSQL数据库生态构建
        
        </a>
    </li>
     
  
 

            
          
        
        </ul>
              
    </li>
  
 

          
         
    </ul>

    
    

    
    <section id="prefooter">
      <hr/>
      <ul>
      
        <li>
          <a class="padding">
            <i class="fa fa-fw fa-language"></i>
          <div class="select-style">
            <select id="select-language" onchange="location = this.value;">
          
          
          
              
              
                  
                    
                    
<option id="en" value="/blog/en/material/seata/">English</option>
                    
                  
              
                  
              
          
              
              
                  
              
                  
                    
                    
<option id="cn" value="/blog/cn/material/seata/" selected>简体中文</option>
                    
                  
              
          
        </select>
        <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
          width="255px" height="255px" viewBox="0 0 255 255" style="enable-background:new 0 0 255 255;" xml:space="preserve">
          <g>
            <g id="arrow-drop-down">
              <polygon points="0,63.75 127.5,191.25 255,63.75 		" />
            </g>
          </g>
        </svg>
        </div>
        </a>
        </li>
       
      
      
      </ul>
    </section>
    
    <section id="footer">
      <p></p>

    </section>
  </div>
</nav>





        <section id="body">
        <div id="overlay"></div>
        <div class="padding highlightable">
              
              <div>
                <div id="top-bar">
                
                
                <div id="breadcrumbs" itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
                    <span id="sidebar-toggle-span">
                        <a href="#" id="sidebar-toggle" data-sidebar-toggle="">
                          <i class="fa fa-bars"></i>
                        </a>
                    </span>
                  
                  <span class="links">
                    
          
          
            
            
          
          
            
            
          
          
            <a href='https://shardingsphere.apache.org/blog/cn/'>ShardingSphere</a> > <a href='https://shardingsphere.apache.org/blog/cn/material/'>博文选集</a> > Apache ShardingSphere整合Seata AT分布式事务
          
         
          
         
          
           
                  </span>
                </div>
                
              </div>
            </div>
            

        
          <div id="chapter">
        
        <div id="body-inner">
          

        


<nav id="TableOfContents">
  <ul>
    <li><a href="#apache-shardingsphere整合seata-at分布式事务">Apache ShardingSphere整合Seata AT分布式事务</a>
      <ul>
        <li><a href="#背景知识">背景知识</a></li>
        <li><a href="#seata-at模型">Seata AT模型</a></li>
        <li><a href="#shardingsphere分布式事务spi">ShardingSphere分布式事务SPI</a></li>
        <li><a href="#整合方案">整合方案</a></li>
        <li><a href="#quick-start">Quick Start</a></li>
        <li><a href="#待优化项">待优化项</a></li>
      </ul>
    </li>
  </ul>
</nav>

<h2 id="apache-shardingsphere整合seata-at分布式事务">Apache ShardingSphere整合Seata AT分布式事务</h2>
<h3 id="背景知识">背景知识</h3>
<p>Seata是阿里集团和蚂蚁金服联合打造的分布式事务框架，目前版本包含了AT事务和TCC事务。其中AT事务的目标是在微服务架构下，提供增量的事务ACID语意，让用户像使用本地事务一样，使用分布式事务，核心理念同ShardingSphere一脉相承。</p>
<p>Github: <a href="https://github.com/seata/seata">https://github.com/seata/seata</a></p>
<h3 id="seata-at模型">Seata AT模型</h3>
<p>Seata AT事务模型包含TM(事务管理器)，RM(资源管理器)，TC(事务协调器)。</p>
<p>其中TC是一个独立的服务需要单独部署，TM和RM以jar包的方式同业务应用部署在一起，它们同TC建立长连接，在整个事务生命周期内，保持RPC通信。</p>
<p>其中全局事务的发起方作为TM，全局事务的参与者作为RM ; TM负责全局事务的begin和commit/rollback，RM负责分支事务的执行和commit/rollback。</p>
<p><img src="https://shardingsphere.apache.org/blog/img/seata1.jpg" alt=""></p>
<h3 id="shardingsphere分布式事务spi">ShardingSphere分布式事务SPI</h3>
<p>ShardingSphere提供了一套接入分布式事务的SPI，设计的目标是保证数据分片后，事务的ACID语意。分布式事务的实现目前主要包含两阶段的XA和BASE柔性事务。Seata AT事务作为BASE柔性事务的一种实现，可以无缝接入到ShardingSphere生态中。</p>
<p><img src="https://shardingsphere.apache.org/blog/img/seata2.jpg" alt=""></p>
<p>两阶段XA事务方面，我们已经整合了Atomikos，Narayana，Bitronix事务管理器，XA事务底层依赖具体的数据库厂商对XA两阶段提交协议的支持，通常XA协议通过在Prepare和Commit阶段进行2PL(2阶段锁)，保证了分布式事务的ACID，通常适用于短事务及非云化环境（云化环境下一次IO操作大概需要20ms，两阶段锁会锁住资源长达40ms，因此事务的TPS会降到25/s左右，非云化环境通常一次IO只需几毫秒，因此锁热点数据的时间相对较低）[1]。</p>
<p>BASE柔性事务方面，目前我们已经完成了对ServiceComb Saga的整合，Saga通过一阶段提交+补偿的方式提高了整体事务的性能，其中补偿的方式同Seata大致相同，即对分片后的物理SQL进行revert来生成补偿的SQL，但Saga模型在理论上不支持隔离级别，适用于对性能要求较高，对一致性要求比较低的业务。Seata AT事务在一阶段提交+补偿的基础上，通过TC的全局锁实现了RC隔离级别的支持，是介于XA和Saga之间的另一种实现。消息柔性事务方面，也欢迎大家参考我们的SPI提供整合的方案。</p>
<h3 id="整合方案">整合方案</h3>
<p>整合Seata AT事务时，需要把TM，RM，TC的模型融入到ShardingSphere 分布式事务的SPI的生态中。在数据库资源上，Seata通过对接DataSource接口，让JDBC操作可以同TC进行RPC通信。同样，ShardingSphere也是面向DataSource接口对用户配置的物理DataSource进行了聚合，因此把物理DataSource二次包装为Seata的DataSource后，就可以把Seata AT事务融入到ShardingSphere的分片中。</p>
<p>在Seata模型中，全局事务的上下文存放在线程变量中，通过扩展服务间的transport，可以完成线程变量的传递，分支事务通过线程变量判断是否加入到整个Seata全局事务中。而ShardingSphere的分片执行引擎通常是按多线程执行，因此整合Seata AT事务时，需要扩展主线程和子线程的事务上下文传递，这同服务间的上下文传递思路完全相同。</p>
<p><img src="https://shardingsphere.apache.org/blog/img/seata3.jpg" alt=""></p>
<h3 id="quick-start">Quick Start</h3>
<p>我们已经实现了base-seata-raw-jdbc-example，大家可以自行进行尝试。</p>
<p><a href="https://github.com/apache/incubator-shardingsphere-example/tree/dev/sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example">https://github.com/apache/incubator-shardingsphere-example/tree/dev/sharding-jdbc-example/transaction-example/transaction-base-seata-example/transaction-base-seata-raw-jdbc-example</a></p>
<p>操作手册：</p>
<p>1.按照seata-work-shop中的步骤，下载并启动seata server。</p>
<p><a href="https://github.com/seata/seata-workshop">https://github.com/seata/seata-workshop</a></p>
<p>参考 Step6 和 Step7即可</p>
<p>2.在每一个分片数据库实例中执行resources/sql/undo_log.sql脚本，创建undo_log表</p>
<p>3.Run YamlConfigurationTransactionExample.java</p>
<h3 id="待优化项">待优化项</h3>
<p>Seata AT事务在Revert SQL时，需要对ShardingSphere分片后的物理SQL进行二次的解析，这里我们需要设计一个SPI，避免SQL二次解析的性能损耗。</p>
<p>参考论文</p>
<p>[1]: Transactions for Distributed Actors in the Cloud</p>
<p><a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2016/10/EldeebBernstein-TransactionalActors-MSR-TR-1.pdf">https://www.microsoft.com/en-us/research/wp-content/uploads/2016/10/EldeebBernstein-TransactionalActors-MSR-TR-1.pdf</a></p>


<footer class=" footline" >
	
</footer>


        
            </div> 
        
        </div> 
        

      </div>

    <div id="navigation">
        
        
        
        
            
            
                
                    
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                    
                
                

                    
                    
                    

                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
            
        
        
        


        
            <a class="nav nav-prev" href="https://shardingsphere.apache.org/blog/cn/material/cncf/" title="快讯！Apache ShardingSphere进入CNCF全景图"> <i class="fa fa-chevron-left"></i></a>
        
        
            <a class="nav nav-next" href="https://shardingsphere.apache.org/blog/cn/material/solution/" title="刚柔并济的开源分布式事务解决方案" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
        
    </div>

    </section>
    
    <div style="left: -1000px; overflow: scroll; position: absolute; top: -1000px; border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;">
      <div style="border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;"></div>
    </div>
    <script src="https://shardingsphere.apache.org/blog/js/clipboard.min.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/perfect-scrollbar.min.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/perfect-scrollbar.jquery.min.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/jquery.sticky.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/featherlight.min.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/html5shiv-printshiv.min.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/highlight.pack.js?1596123313"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <script src="https://shardingsphere.apache.org/blog/js/modernizr.custom.71422.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/learn.js?1596123313"></script>
    <script src="https://shardingsphere.apache.org/blog/js/hugo-learn.js?1596123313"></script>

    

  </body>
</html>

